package com.zmn.mcc.admin.controller.staff.dpermit;

import com.zmn.common.dictionary.GlobalDict;
import com.zmn.common.dto2.ResponseDTO;
import com.zmn.common.utils.request.RequestUtil;
import com.zmn.common.utils.string.StringUtil;
import com.zmn.cube.common.dro.measure.MeasureDRO;
import com.zmn.cube.common.dro.menu.MenuDRO;
import com.zmn.cube.dubbo.dto.DubboConsts;
import com.zmn.cube.dubbo.interfaces.measure.MeasureListRemoteService;
import com.zmn.cube.dubbo.interfaces.menu.MenuListRemoteService;
import com.zmn.mcc.common.constant.DpermitConsts;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @author heguanghua
 * @since 2020/05/20 12:28
 */
@Controller
@RequestMapping("/staff/dpermit/modify/")
public class StaffDpermitModifyController extends StaffDpermitBaseController {

    @DubboReference(version = DubboConsts.INTERFACE_VERSION, check = false)
    private MenuListRemoteService menuListRemoteService;
    @DubboReference(version = DubboConsts.INTERFACE_VERSION, check = false)
    private MeasureListRemoteService measureListRemoteService;

    // region 组织数据权限

    /**
     * 为指定员工分配平台/大区/子公司数据权限
     */
    @RequestMapping("organize/assign")
    @ResponseBody
    public ResponseDTO assignOrganize(HttpServletRequest request, HttpServletResponse response,
                                      @RequestParam(value = "staffId", defaultValue = "0") Integer staffId,
                                      @RequestParam(value = "dpermitIds", defaultValue = "") String dpermitIds) {

        String ip = RequestUtil.getRequestIp(request);
        ResponseDTO result = staffDpermitBService.assignOrganizeDpermit(staffId, StringUtil.split(dpermitIds, Integer.class), getLoginStaff(), ip);
        return result;
    }

    /**
     * 为指定员工分配推广数据权限
     */
    @RequestMapping("promotion/assign")
    @ResponseBody
    public ResponseDTO assignPromotion(HttpServletRequest request, HttpServletResponse response,
                                       @RequestParam(value = "keyId", defaultValue = "0") Integer keyId,
                                       @RequestParam(value = "dpermitIds", defaultValue = "") String dpermitIds,
                                       @RequestParam(value = "account", defaultValue = "") String account,
                                       @RequestParam(value = "type", defaultValue = "0") Integer type) {

        String ip = RequestUtil.getRequestIp(request);
        ResponseDTO result = staffDpermitBService.assignPromotionDpermit(keyId, StringUtil.split(dpermitIds, Integer.class),StringUtil.split(account, Integer.class), getLoginStaff(), ip,type);
        return result;
    }

    /**
     * 为指定员工分配渠道数据权限
     */
    @RequestMapping("newchannel/assign")
    @ResponseBody
    public ResponseDTO assignNewchannel(HttpServletRequest request, HttpServletResponse response,
                                     @RequestParam(value = "keyId", defaultValue = "0") Integer keyId,
                                     @RequestParam(value = "dpermitIds", defaultValue = "") String dpermitIds) {

        String ip = RequestUtil.getRequestIp(request);
        ResponseDTO result = staffDpermitBService.assignNewChannelDpermit(keyId, StringUtil.split(dpermitIds, Integer.class), getLoginStaff(), ip);
        return result;
    }

    /**
     * 为指定员工添加服务商数据权限
     */
    @RequestMapping("organize/add")
    @ResponseBody
    public ResponseDTO addOrganize(HttpServletRequest request, HttpServletResponse response,
                                   @RequestParam(value = "keyId", defaultValue = "0") Integer staffId,
                                   @RequestParam(value = "companyIds", defaultValue = "") String companyIds) {

        ResponseDTO result = staffDpermitBService.addOrganizeDpermitForSp(staffId, StringUtil.split(companyIds, Integer.class), getStaffId(), getStaffName());
        return result;
    }

    /**
     * 移除指定员工的指定子公司及其服务商数据权限
     */
    @RequestMapping("organize/delete")
    @ResponseBody
    public ResponseDTO deleteOrganize(HttpServletRequest request, HttpServletResponse response,
                                      @RequestParam(value = "staffId", defaultValue = "0") Integer staffId,
                                      @RequestParam(value = "dataId", defaultValue = "0") Integer dataId) {

        ResponseDTO result = staffDpermitBService.deleteOrganizeDpermit(staffId, dataId, getStaffId(), getStaffName());
        return result;
    }

    /**
     * 移除指定员工的指定服务商数据权限
     */
    @RequestMapping("organize/deleteSp")
    @ResponseBody
    public ResponseDTO deleteSpOrganize(HttpServletRequest request, HttpServletResponse response,
                                        @RequestParam(value = "staffId", defaultValue = "0") Integer staffId,
                                        @RequestParam(value = "dataId", defaultValue = "0") Integer dataId) {

        ResponseDTO result = staffDpermitBService.deleteOrganizeDpermitForSp(staffId, dataId, getStaffId(), getStaffName());
        return result;
    }

    /**
     * 清除指定员工的全部组织数据权限
     */
    @RequestMapping("organize/clear")
    @ResponseBody
    public ResponseDTO clearOrganize(HttpServletRequest request, HttpServletResponse response,
                                     @RequestParam(value = "staffId", defaultValue = "0") Integer staffId) {

        String ip = RequestUtil.getRequestIp(request);
        ResponseDTO result = staffDpermitBService.clearOrganizeDpermit(staffId, getLoginStaff(), ip);
        return result;
    }
    // endregion

    /**
     * 清除指定员工的全部渠道数据权限
     */
    @RequestMapping("newchannel/clear")
    @ResponseBody
    public ResponseDTO clearNewchannel(HttpServletRequest request, HttpServletResponse response,
                                     @RequestParam(value = "staffId", defaultValue = "0") Integer staffId) {

        String ip = RequestUtil.getRequestIp(request);
        ResponseDTO result = staffDpermitBService.clearNewchannelDpermit(staffId, getLoginStaff(), ip);
        return result;
    }

    // region 部门数据权限

    /**
     * 为指定员工分配部门数据权限
     */
    @RequestMapping("dept/assign")
    @ResponseBody
    public ResponseDTO assignDept(HttpServletRequest request, HttpServletResponse response,
                                  @RequestParam(value = "staffId", defaultValue = "0") Integer staffId,
                                  @RequestParam(value = "dpermitIds", defaultValue = "") String dpermitIds) {

        String ip = RequestUtil.getRequestIp(request);
        ResponseDTO result = staffDpermitBService.assignDeptDpermit(staffId, StringUtil.split(dpermitIds, Integer.class), getLoginStaff(), ip);
        return result;
    }

    /**
     * 清除指定员工的全部部门数据权限
     */
    @RequestMapping("dept/clear")
    @ResponseBody
    public ResponseDTO clearDept(HttpServletRequest request, HttpServletResponse response,
                                 @RequestParam(value = "staffId", defaultValue = "0") Integer staffId) {

        String ip = RequestUtil.getRequestIp(request);
        ResponseDTO result = staffDpermitBService.clearDeptDpermit(staffId, getLoginStaff(), ip);
        return result;
    }
    // endregion

    // region 区域数据权限

    /**
     * 为指定员工分配区域数据权限
     */
    @RequestMapping("area/assign")
    @ResponseBody
    public ResponseDTO assignArea(HttpServletRequest request, HttpServletResponse response,
                                  @RequestParam(value = "staffId", defaultValue = "0") Integer staffId,
                                  @RequestParam(value = "dpermitIds", defaultValue = "") String dpermitIds) {

        String ip = RequestUtil.getRequestIp(request);
        ResponseDTO result = staffDpermitBService.assignAreaDpermit(staffId, StringUtil.split(dpermitIds, Integer.class), getLoginStaff(), ip);
        return result;
    }

    /**
     * 清除指定员工的全部区域数据权限
     */
    @RequestMapping("area/clear")
    @ResponseBody
    public ResponseDTO clearArea(HttpServletRequest request, HttpServletResponse response,
                                 @RequestParam(value = "staffId", defaultValue = "0") Integer staffId) {

        String ip = RequestUtil.getRequestIp(request);
        ResponseDTO result = staffDpermitBService.clearAreaDpermit(staffId, getLoginStaff(), ip);
        return result;
    }
    // endregion

    // region 渠道数据权限

    /**
     * 为指定员工分配下单平台数据权限
     */
    @RequestMapping("channel/assignPlat")
    @ResponseBody
    public ResponseDTO assignChannelPlat(HttpServletRequest request, HttpServletResponse response,
                                         @RequestParam(value = "staffId", defaultValue = "0") Integer staffId,
                                         @RequestParam(value = "dpermitIds", defaultValue = "") String plats) {

        String ip = RequestUtil.getRequestIp(request);
        ResponseDTO result = staffDpermitBService.assignChannelDpermitForPlat(staffId, StringUtil.split(plats, Integer.class), getLoginStaff(), ip);
        return result;
    }

    /**
     * 为指定员工添加渠道数据权限
     */
    @RequestMapping("channel/add")
    @ResponseBody
    public ResponseDTO addChannel(HttpServletRequest request, HttpServletResponse response,
                                  @RequestParam(value = "keyId", defaultValue = "0") Integer staffId,
                                  @RequestParam(value = "channelIds", defaultValue = "") String channelIds) {

        String ip = RequestUtil.getRequestIp(request);
        ResponseDTO result = staffDpermitBService.addChannelDpermit(staffId, StringUtil.split(channelIds, Integer.class), getLoginStaff(), ip);
        return result;
    }

    /**
     * 移除指定员工的指定平台及其渠道数据权限
     */
    @RequestMapping("channel/deletePlat")
    @ResponseBody
    public ResponseDTO deleteChannelPlat(HttpServletRequest request, HttpServletResponse response,
                                         @RequestParam(value = "staffId", defaultValue = "0") Integer staffId,
                                         @RequestParam(value = "dataId", defaultValue = "0") Integer plat) {

        String ip = RequestUtil.getRequestIp(request);
        ResponseDTO result = staffDpermitBService.deleteChannelDpermit(staffId, plat, getLoginStaff(), ip);
        return result;
    }

    /**
     * 移除指定员工的指定渠道数据权限
     */
    @RequestMapping("channel/delete")
    @ResponseBody
    public ResponseDTO deleteChannel(HttpServletRequest request, HttpServletResponse response,
                                     @RequestParam(value = "staffId", defaultValue = "0") Integer staffId,
                                     @RequestParam(value = "dataIds", defaultValue = "") String channelIds) {

        String ip = RequestUtil.getRequestIp(request);
        ResponseDTO result = staffDpermitBService.deleteChannelDpermit(staffId, StringUtil.split(channelIds, Integer.class), getLoginStaff(), ip);
        return result;
    }

    /**
     * 清除指定员工的全部渠道数据权限
     */
    @RequestMapping("channel/clear")
    @ResponseBody
    public ResponseDTO clearChannel(HttpServletRequest request, HttpServletResponse response,
                                    @RequestParam(value = "staffId", defaultValue = "0") Integer staffId) {

        String ip = RequestUtil.getRequestIp(request);
        ResponseDTO result = staffDpermitBService.clearChannelDpermit(staffId, getLoginStaff(), ip);
        return result;
    }
    // endregion

    // region 大数据报表数据权限

    /**
     * 为指定员工分配大数据报表数据权限
     */
    @RequestMapping("cube/assign")
    @ResponseBody
    public ResponseDTO assignCube(HttpServletRequest request, HttpServletResponse response,
                                  @RequestParam(value = "staffId", defaultValue = "0") Integer staffId,
                                  @RequestParam(value = "dpermitIds", defaultValue = "") String dpermitIds) {

        String ip = RequestUtil.getRequestIp(request);
        ResponseDTO<List<MenuDRO>> menusResponse = menuListRemoteService.listAboveByLevel(DpermitConsts.CUBE_LEVEL_FOUR, GlobalDict.STATUS_ENABLE);
        if (!menusResponse.isSuccess()) {
            return ResponseDTO.fail("没有取得菜单列表");
        }
        ResponseDTO result = staffDpermitBService.assignCubeDpermit(staffId, StringUtil.split(dpermitIds, Integer.class), menusResponse.getData(), getLoginStaff(), ip);
        return result;
    }

    /**
     * 清除指定员工的全部大数据报表数据权限
     */
    @RequestMapping("cube/clear")
    @ResponseBody
    public ResponseDTO clearCube(HttpServletRequest request, HttpServletResponse response,
                                 @RequestParam(value = "staffId", defaultValue = "0") Integer staffId) {

        String ip = RequestUtil.getRequestIp(request);
        ResponseDTO result = staffDpermitBService.clearCubeDpermit(staffId, getLoginStaff(), ip);
        return result;
    }
    // endregion

    /**
     * 为指定员工分配大数据报表指标数据权限
     */
    @RequestMapping("measure/assign")
    @ResponseBody
    public ResponseDTO assignMeasure(HttpServletRequest request, HttpServletResponse response,
                                     @RequestParam(value = "staffId", defaultValue = "0") Integer staffId,
                                     @RequestParam(value = "dpermitIds", defaultValue = "") String dpermitIds) {

        String ip = RequestUtil.getRequestIp(request);
        ResponseDTO<List<MeasureDRO>> menusResponse = measureListRemoteService.listMeasureByDataset(GlobalDict.STATUS_ENABLE);
        if (!menusResponse.isSuccess()) {
            return ResponseDTO.fail("没有取得指标列表");
        }
        ResponseDTO result = staffDpermitBService.assignMeasureDpermit(staffId, StringUtil.split(dpermitIds, Integer.class), menusResponse.getData(), getLoginStaff(), ip);
        return result;
    }

    /**
     * 清除指定员工的全部大数据报表指标数据权限
     */
    @RequestMapping("measure/clear")
    @ResponseBody
    public ResponseDTO clearMeasure(HttpServletRequest request, HttpServletResponse response,
                                    @RequestParam(value = "staffId", defaultValue = "0") Integer staffId) {

        String ip = RequestUtil.getRequestIp(request);
        ResponseDTO result = staffDpermitBService.clearMeasureDpermit(staffId, getLoginStaff(), ip);
        return result;
    }

    // region 产品数据权限

    /**
     * 为指定员工分配产品数据权限
     */
    @RequestMapping("product/assign")
    @ResponseBody
    public ResponseDTO assignProduct(HttpServletRequest request, HttpServletResponse response,
                                     @RequestParam(value = "staffId", defaultValue = "0") Integer staffId,
                                     @RequestParam(value = "dpermitIds", defaultValue = "") String dpermitIds) {

        String ip = RequestUtil.getRequestIp(request);
        ResponseDTO result = staffDpermitBService.assignProductDpermit(staffId, StringUtil.split(dpermitIds, String.class), getLoginStaff(), ip);
        return result;
    }

    /**
     * 清除指定员工的全部产品数据权限
     */
    @RequestMapping("product/clear")
    @ResponseBody
    public ResponseDTO clearProduct(HttpServletRequest request, HttpServletResponse response,
                                    @RequestParam(value = "staffId", defaultValue = "0") Integer staffId) {

        String ip = RequestUtil.getRequestIp(request);
        ResponseDTO result = staffDpermitBService.clearProductDpermit(staffId, getLoginStaff(), ip);
        return result;
    }

    /**
     * 清除指定员工的全部组织数据权限
     */
    @RequestMapping("promotion/clear")
    @ResponseBody
    public ResponseDTO clearPromotion(HttpServletRequest request, HttpServletResponse response,
                                      @RequestParam(value = "staffId", defaultValue = "0") Integer staffId) {

        String ip = RequestUtil.getRequestIp(request);
        ResponseDTO result = staffDpermitBService.clearPromotionDpermit(staffId, getLoginStaff(), ip);
        return result;
    }
}
